package org.cc.cloud.controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.cc.cloud.entity.Order;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
public class RabbitmqController {
	@Autowired
	private RabbitTemplate rbbitTemplate;
	@Autowired
	private RabbitTemplate rabbitTemplate1;
	@GetMapping("/sendJosn")
	public void sendJosn() {
		Order order = new Order();
		order.setId("1");
		order.setName("测试商品");
		order.setTime(LocalDateTime.now().toString());
		ObjectMapper mapper = new ObjectMapper();
		String json;
		try {
			json = mapper.writeValueAsString(order);
			MessageProperties pro = new MessageProperties();
			pro.setContentType("application/json");
			Message message = new Message(json.getBytes(), pro);
			rbbitTemplate.send("order", null, message);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	//指定消费者为java类
	@GetMapping("/sendJosnToClass")
	//消费者请查看rabbitmq-consumer/RabbitMqConfig/messageListenerAdapter
	//rabbitmq-consumer/RabbitMqConfig/smsMessage(List<Order> body)
	public void sendJosnToClass() {
		Order order = new Order();
		order.setId("1");
		order.setName("测试商品");
		order.setTime(LocalDateTime.now().toString());
		ObjectMapper mapper = new ObjectMapper();
		String json;
		try {
			json = mapper.writeValueAsString(order);
			MessageProperties pro = new MessageProperties();
			pro.setContentType("application/json");
			//指定的是消费者那边的java类
//			pro.getHeaders().put("__TypeId__", "org.cc.cloud.entity.Order");
			//此方法要配合消费者的JavaTypeMapper一起使用
			pro.getHeaders().put("__TypeId__", "order");
		
			Message message = new Message(json.getBytes(), pro);
			rbbitTemplate.send("sms", "sms", message);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	//指定消费者为List
	//消费者请查看rabbitmq-consumer /RabbitMqConfig/messageListenerAdapter
	//rabbitmq-consumer/RabbitMqConfig/smsMessage(List<Order> body)
	@GetMapping("/sendJosnToClassList")
	public void sendJosnToClassList() {
		List<Order>  list=new  ArrayList<Order>();
		Order order = new Order();
		order.setId("1");
		order.setName("测试商品");
		order.setTime(LocalDateTime.now().toString());
		list.add(order);
		ObjectMapper mapper = new ObjectMapper();
		String json;
		try {
			json = mapper.writeValueAsString(list);
			MessageProperties pro = new MessageProperties();
			pro.setContentType("application/json");
			//指定的是消费者那边的java类
			pro.getHeaders().put("__TypeId__", "java.util.List");
			pro.getHeaders().put("__ContentTypeId__", "order");
			//如果是Map<String,Order>
//			pro.getHeaders().put("__TypeId__", "java.util.List");
//			pro.getHeaders().put("__ContentTypeId__", "order");
//			pro.getHeaders().put("__KeyTypeId__", "java.lang.String");
			Message message = new Message(json.getBytes(), pro);
			rbbitTemplate.send("sms", "sms", message);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	//测试发送不能路由的消息
	@GetMapping("/sendJosn1")
	public void sendJosnTo1() {
		MessageProperties pro = new MessageProperties();
		pro.setContentType("text");
		Message message = new Message("1231313".getBytes(), pro);
		CorrelationData correlationData =new CorrelationData();
		correlationData.setId("1");
		//.send(exchange, routingKey, message, correlationData);
		rabbitTemplate1.send(message);
		rabbitTemplate1.send("sms", "sms1", message,correlationData);
	}
}
